package com.winter.admin.controller;

import com.winter.admin.entity.dto.MenuVO;
import com.winter.admin.service.MenuService;
import com.winter.common.dto.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangshuaiyin
 * @date 2021-06-21 14:04
 */
@Api(value = "菜单接口", tags = "菜单接口")
@RestController
public class MenuController {

    private final MenuService menuService;

    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    @ApiOperation(value = "获取菜单树", notes = "获取多级菜单")
    @GetMapping("/menus")
    public BaseResponse<List<MenuVO>> getMenuList() {
        List<MenuVO> menuTrees = menuService.getMenuTrees();
        return BaseResponse.ok(menuTrees);
    }

    @ApiOperation(value = "根据id获取菜单", notes = "根据id获取菜单")
    @GetMapping("/menus/{id}")
    public BaseResponse<MenuVO> getMenuById(@PathVariable Integer id) {
        return BaseResponse.ok(menuService.getMenuById(id));
    }

    @ApiOperation(value = "新增菜单", notes = "新增菜单")
    @PostMapping("/menus")
    public BaseResponse<?> addMenu(@RequestBody MenuVO menuVO) {
        menuService.addMenu(menuVO);
        return BaseResponse.ok();
    }

    @ApiOperation(value = "修改菜单", notes = "修改菜单")
    @PutMapping("/menus")
    public BaseResponse<?> updateMenu(@RequestBody MenuVO menuVO) {
        menuService.updateMenu(menuVO);
        return BaseResponse.ok();
    }

    @ApiOperation(value = "删除菜单", notes = "删除菜单")
    @DeleteMapping("/menus/{id}")
    public BaseResponse<?> deleteMenu(@PathVariable Integer id) {
        menuService.deleteMenu(id);
        return BaseResponse.ok();
    }
}
